﻿using System;
using System.Collections.Generic;
using System.Globalization;
using Medianamik.Core;
using Medianamik.Core.DAL;
using Medianamik.Core.DAL.Services;
using Medianamik.Core.Profile;
using Medianamik.UI.Web.Membership.Data.Repositories;
using SubSonic;

namespace Medianamik.DummyWebSite.Data.Repositories
{
    public class UserProfileRepository : UserProfileRepository<UserProfileCollection, 
        UserProfile>
    {
        public UserProfileRepository(ICultureService cultureService, 
            IContentStateService contentStateService, INodeMapper nodeMapper)
            : base(cultureService, contentStateService, nodeMapper)
        {
        }
    }

    public class UserProfileResover : IUserProfileResolver
    {
        private readonly IUserProfileRepository<UserProfile> _userProfileRepository;

        public UserProfileResover(IUserProfileRepository<UserProfile> userProfileRepository)
        {
            _userProfileRepository = userProfileRepository;
        }

        public IUserProfileRepository<UserProfile> UserProfileRepository
        {
            get { return _userProfileRepository; }
        }

        public Guid? GetUserProfileIdByUserId(Guid userId)
        {
            var where = new Where { ColumnName = "User", Comparison = Comparison.Equals, ParameterValue = userId };

            var profiles = UserProfileRepository.GetWithFilters(CultureInfo.InvariantCulture, ContentState.Draft, ActiveStatus.All,
                MedianamikTypes.MedianamikProfile, MedianamikModules.SecurityManagement, new List<Where> { where }, null, null, null, true);

            return profiles.Count > 0 ? (Guid?)profiles[0].NodeId : null;
        }
    }
}